//
//  MenuPopup.swift
//  Automobile
//
//  Created by erkebulan elzhan on 8/15/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import UIKit
import SnapKit


final class MenuPopup: UIView {
    
    struct MenuItem {
        var icon: UIImage?
        var title: String
    }
    
    // MARK: - Outlets
    
    private lazy var container: TriangleView = {
        let view = TriangleView()
        return view
    }()
    
    private lazy var menuStack: UIStackView = {
        let stack = UIStackView()
        stack.axis = .vertical
        stack.distribution = .equalSpacing
        stack.alignment = .fill
        stack.spacing = 20
        return stack
    }()
    
    
    @objc private func onItemClick (sender: GestureWithTag) {
        NotificationCenter.default.post(name: NSNotification.Name("menuClick"), object: nil, userInfo: ["index": sender.tag - 1])
    }
    
    // MARK: - Init
    
    init (frame: CGRect = .zero, size: CGSize = CGSize(width: 0, height: 0)) {
        super.init(frame: frame)
        self.addViews(frame: frame, size: size)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public func addButtons (buttons: [MenuItem]) {
        self.addMenuButtons(buttons: buttons)
    }
}

extension MenuPopup {
    
    private func addViews (frame: CGRect, size: CGSize) {
        self.addSubview(container)
        self.addSubview(menuStack)
        container.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        menuStack.snp.makeConstraints { (make) in
            make.top.bottom.equalToSuperview().inset(32)
            make.leading.trailing.equalToSuperview().inset(24)
        }
        container.drawView(in: CGRect(x: 0, y: 0, width: frame != .zero ? frame.width : size.width, height: frame != .zero ? frame.height : size.height))
    }
    
    private func addMenuButtons (buttons: [MenuItem]) {
        for (i, button) in buttons.enumerated() {
            let view = self.createMenuButton(item: button)
            let tapGesture = GestureWithTag(target: self, action: #selector(onItemClick(sender:)))
            tapGesture.tag = i + 1
            view.addGestureRecognizer(tapGesture)
            
            view.snp.makeConstraints { (make) in
                make.height.equalTo(30)
            }
            
            menuStack.addArrangedSubview(view)
        }
    }
    
    private func createMenuButton (item: MenuItem) -> UIView {
        let view = UIView()
        
        let iconImg = UIImageView()
        iconImg.image = item.icon
        
        let textLabel = UILabel()
        textLabel.font = .systemFont(ofSize: 15, weight: .medium)
        textLabel.textColor = .black
        textLabel.sizeToFit()
        textLabel.textAlignment = .left
        textLabel.text = item.title
        
        view.addSubview(iconImg)
        view.addSubview(textLabel)

        iconImg.snp.makeConstraints { (make) in
            make.width.height.equalTo(30)
            make.leading.top.equalToSuperview()
        }
        textLabel.snp.makeConstraints { (make) in
            make.leading.equalTo(iconImg.snp.trailing).offset(8)
            make.centerY.trailing.equalToSuperview()
        }
        return view
    }
}



final class TriangleView: UIView {
    
    private let inset: CGFloat = 8.0
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    public func drawView (in rect: CGRect) {
        self.setup(rect: rect)
    }
    
    
    private func createBezierPath(rect: CGRect) -> UIBezierPath {

        let path = UIBezierPath()
        
        // initial point
        path.move(to: CGPoint(x: inset, y: inset))
    
        // top left
        path.addArc(withCenter: CGPoint(x: inset, y: inset), radius: inset, startAngle: CGFloat(Double.pi), endAngle: CGFloat(Double.pi * 1.5), clockwise: true)
        path.addLine(to: CGPoint(x: rect.maxX - (6 * inset - 4), y: 0))
    
        // Arrow Top
        path.addLine(to: CGPoint(x: rect.maxX - (5 * inset - 4), y: -7))
        path.addLine(to: CGPoint(x: rect.maxX - (4 * inset - 4), y: 0))
        path.addLine(to: CGPoint(x: rect.maxX - 4 * inset, y: 0))
    
        // top right
        path.addArc(withCenter: CGPoint(x: rect.maxX - 4 * inset, y: inset), radius: inset, startAngle: CGFloat(Double.pi * 1.5), endAngle: 0, clockwise: true)
        path.addLine(to: CGPoint(x: rect.maxX - 3 * inset, y: rect.maxY - 4 * inset))
        
        // bottom right
        path.addArc(withCenter: CGPoint(x: rect.maxX - 4 * inset, y: rect.maxY - 4 * inset), radius: inset, startAngle: 0, endAngle: CGFloat(Double.pi/2), clockwise: true)
        path.addLine(to: CGPoint(x: inset, y: rect.maxY - 3 * inset))
        
        // bottom left
        path.addArc(withCenter: CGPoint(x: inset, y: rect.maxY - 4 * inset), radius: inset, startAngle: CGFloat(Double.pi/2), endAngle: CGFloat(Double.pi), clockwise: true)
        path.addLine(to: CGPoint(x: 0, y: inset))
        
        path.close()
        return path
    }
    
    private func setup(rect: CGRect) {
        let shapeLayer = CAShapeLayer()
        shapeLayer.path = createBezierPath(rect: rect).cgPath
        shapeLayer.fillColor = UIColor.white.cgColor
        shapeLayer.position = CGPoint(x: 10, y: 10)
        self.layer.addSublayer(shapeLayer)
    }
}

